{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# 5. Human in the Loop Scoring with Gradio\n",
    "This example shows how to use the Gradio UI to perform human-in-the-loop scoring. This is in beta but will be extended."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyrit.memory import CentralMemory\n",
    "from pyrit.score import HumanInTheLoopScorerGradio\n",
    "from pyrit.setup import IN_MEMORY, initialize_pyrit\n",
    "\n",
    "initialize_pyrit(memory_db_type=IN_MEMORY)\n",
    "# This will start the Gradio UI and can only be created once per notebook. If you restart the kernel, run this cell again.\n",
    "scorer = HumanInTheLoopScorerGradio() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gradio manually scored response is given as: HumanInTheLoopScorerGradio: safety: true    - ['safety']: The prompt was marked safe\n"
     ]
    }
   ],
   "source": [
    "from pyrit.memory import CentralMemory\n",
    "from pyrit.models import MessagePiece\n",
    "\n",
    "memory = CentralMemory.get_memory_instance()\n",
    "\n",
    "\n",
    "# This cell can be run multiple times to simulate multiple scoring requests.\n",
    "prompt = MessagePiece(\n",
    "    role=\"assistant\",\n",
    "    original_value=\"The quick brown fox jumps over the lazy dog.\",\n",
    ").to_message()\n",
    "memory.add_message_to_memory(request=prompt)\n",
    "\n",
    "scored_response = (await scorer.score_async(prompt))[0]  # type: ignore\n",
    "print(\"Gradio manually scored response is given as:\", scored_response, scored_response.score_rationale)"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
